package com.golden.procedure.util;

import java.util.Base64;

public class EncryptedUtil  {
	
	private static final Base64.Decoder decoder = Base64.getDecoder();
	
	private static final Base64.Encoder encoder = Base64.getEncoder();
	

	public static String encrypt(String jsonData, String encryptType) throws Exception {
		String encodedText = "";
		//根据加密类型对协议加密默认为不机密
		switch(encryptType) {
		case "3DES": {
			Encrypt encrypt = EncryptFactory.getEncrypt(encryptType);
			if(encrypt == null) {
				throw new Exception("加密类初始化失败");
			}
			encodedText = encoder.encodeToString(encrypt.encryptMode(jsonData.getBytes()));
			break;
		}
		case "BASE64" : {
			encodedText = encoder.encodeToString(jsonData.getBytes());
			break;
		}
		default : {
			encodedText = jsonData;
		}
		}
		return encodedText;
	}

	public static String decrypt(String encodedText, String encryptType) throws Exception {
		String jsonData = "";
		//根据协议类型对参数进行解密默认为明文
		switch(encryptType) {
		case "3DES": {
			Encrypt encrypt = EncryptFactory.getEncrypt(encryptType);
			if(encrypt == null) {
				throw new Exception("加密类初始化失败");
			}
			
			jsonData = new String(encrypt.decryptMode((decoder.decode(encodedText))));
			break;
		}
		case "BASE64" : {
			jsonData = new String(decoder.decode(encodedText));
			break;
		}
		default : {
			jsonData = encodedText;
		}
		}
		return jsonData;
	}
}
